package com.woniu.controller.mallSystem;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.model.gh.common.Result;
import com.woniu.model.gh.common.enums.impl.ResultCode;
import com.woniu.model.gh.utils.BatchDownload;
import com.woniu.model.mallSystem.dto.OrderDto;
import com.woniu.model.mallSystem.gh.annotation.Idempotent;
import com.woniu.model.mallSystem.gh.common.RedisConstant;
import com.woniu.model.mallSystem.vo.OrderVo;
import com.woniu.service.mallSystem.IOrderService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/goodsOrder")
public class GoodsOrderController {

    private final IOrderService orderServiceImpl;
    private final RedisTemplate redisTemplate;

    public GoodsOrderController(IOrderService orderServiceImpl, RedisTemplate redisTemplate){
        this.orderServiceImpl = orderServiceImpl;
        this.redisTemplate = redisTemplate;
    }

//    @Idempotent(expireTime = 15, timeunit = TimeUnit.SECONDS)
    @PostMapping("/download")
    public void download(@RequestBody List<Integer> ids,
                           HttpServletResponse resp) throws IOException {

        // rawFileName 设置excel文件名
        BatchDownload.setExcelRespProp(resp, "订单信息");

        List<OrderDto> orderDto = orderServiceImpl.getByIds(ids);

        EasyExcel.write(resp.getOutputStream())
                .head(OrderDto.class)
                .excelType(ExcelTypeEnum.XLSX) // 设置格式为xlsx
                .sheet("订单信息")  // 设置工作簿名
                .doWrite(orderDto);
    }

    // 查询全部订单
    @GetMapping("/getPageInfo")
    public Result getAllOrders(@RequestParam(defaultValue = "5") Integer pageSize,
                               @RequestParam(defaultValue = "1") Integer pageNum, Integer orderStatus){
        Page<OrderVo> pageInfo = orderServiceImpl.getPageInfo(pageSize, pageNum, orderStatus);
        return Result.ok(pageInfo);
    }

    // 关闭订单
    @Idempotent(expireTime = 15, timeunit = TimeUnit.SECONDS)
    @DeleteMapping("/close/{id}")
    public Result close(@PathVariable("id") Integer id){

        // 接口幂等性
        if (redisTemplate.hasKey(RedisConstant.INTERFACE_IDEA_PREFIX + id)) {
            //请求已处理，请勿重复提交4000
            return Result.fail(ResultCode.HTTP_ALREADY_HANDLE);
        }
        redisTemplate.opsForValue().setIfAbsent(RedisConstant.INTERFACE_IDEA_PREFIX + id,
                        "", 30, TimeUnit.SECONDS);
        orderServiceImpl.removeById(id);
        return Result.ok();
    }

    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable("id") Integer orderId){
        OrderVo goodsOrderVO = orderServiceImpl.selectById(orderId);
        return Result.ok(goodsOrderVO);
    }
}
